﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SwitchTab : MonoBehaviour
{
    public GameObject tab1;
    public GameObject[] tab1Objs;
    public GameObject tab2;
    public GameObject[] tab2Objs;
    Vector3 tab1Pos;
    Vector3 tab2Pos;
    Vector3 outPos = Vector3.one * -1000f;
    public Toggle toggle;
    public RectTransform rect1;
    public RectTransform rect2;
    void Start()
    {
        toggle = GetComponentInChildren<Toggle>();
        toggle.onValueChanged.AddListener(OnChanged);
        //tab1Pos = tab1.transform.position;
        //tab2Pos = tab2.transform.position;
        rect1 = tab1.GetComponent<RectTransform>();
        rect2 = tab2.GetComponent<RectTransform>();
        tab1Pos = tab1.GetComponent<RectTransform>().anchoredPosition3D;
        tab2Pos = tab2.GetComponent<RectTransform>().anchoredPosition3D;
        tab2.transform.position = outPos;
    }
    void OnChanged(bool value)
    {
        if (value)
        {
            rect1.anchoredPosition3D = outPos;
            rect2.anchoredPosition3D = tab2Pos;
            foreach (var obj in tab1Objs)
                if (obj != null) obj.SetActive(false);
            foreach (var obj in tab2Objs)
                if (obj != null) obj.SetActive(true);
            var sts = tab2.GetComponentsInChildren<SwitchTab>();
            foreach (var st in sts)
            {
                st.OnChanged(st.toggle.isOn);
            }
        }
        else
        {
            rect1.anchoredPosition3D = tab1Pos;
            rect2.anchoredPosition3D = outPos;
            foreach (var obj in tab1Objs)
                if (obj != null) obj.SetActive(true);
            foreach (var obj in tab2Objs)
                if (obj != null) obj.SetActive(false);
            var sts = tab1.GetComponentsInChildren<SwitchTab>();
            foreach (var st in sts)
            {
                st.OnChanged(st.toggle.isOn);
            }
        }

    }

}
